Skip to content

Conversation

@sohanv
Copy link

@sohanv sohanv commented Jun 21, 2025

Fixes issue #21 "to create an automatic symbolic map for enum values."

[Clang] Add support for -symbolic-map to automatically generate symbolic name tables for enumerators

Summary

This patch introduces a new Clang option -symbolic-map that enables automatic generation of global string name tables for C/C++ enumerators. When this flag is enabled, Clang emits a global variable for each named, defined enum (including enum class and enum struct), mapping enumerator values to their symbolic string names.

Example

Given the enum:

enum Day {
    Monday,
    Tuesday,
    Wednesday
};

Clang will emit the following global variable into the object file:

const char *__nameof_Day[] = {
    "Monday",
    "Tuesday",
    "Wednesday"
};

The symbol __nameof_Day will be available in the object file and can be inspected via nm or objdump.

Implementation

CodeGenModule.cpp

  • Added a new method EmitEnumSymbolicMap(const EnumDecl *ED) to emit a global constant array of string literals for the enumerator names.
  • Skips unnamed, empty, or forward-declared enums.
  • Uses GetAddrOfConstantCString to obtain LLVM constant string pointers.
  • The array is emitted with WeakODR linkage and pointer-aligned.

CodeGenModule.h

  • Declared the new function:
    void EmitEnumSymbolicMap(const EnumDecl *ED);

Options.td

  • Introduced a new frontend flag:

    def symbolic_map : Flag<["-"], "symbolic-map">,
      Group<f_Group>,
      Visibility<[ClangOption, CC1Option]>,
      HelpText<"Automatically generate a map of symbolic names for enumerators">,
      MarshallingInfoFlag<LangOpts<"SymbolicMap">>;
    

LangOptions.def

  • Added a new language option:

    LANGOPT(SymbolicMap, 1, 0, "Automatically generate a map of symbolic names for enumerators")

Logic Summary

The symbolic map is generated only if:

  • The enum is named and not anonymous.
  • It is a definition (not a forward declaration).
  • It contains at least one enumerator.

Scoped and unscoped enums are both supported.

Symbol Emission

For the following enum:

enum class Color { Red, Green, Blue };

The object file will contain a global variable named:

__nameof_Color

Output:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant